#include "RTthreads.h"

#include <string.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include "RTthreads.h"
#include "tools_share.h"
#include "sharemem_base.h"

struct cylic_thread_params ctp_fast_task;
struct once_thread_params otp_ECM_INIT;
struct waitsem_thread_params wtsem_slow_follow_task1;
struct waitsem_thread_params wtsem_slow_follow_task2;


void OnceThread_Hardware_Init(struct once_thread_params *f_p)
{
    printf("OnceThread_Hardware_Init-start:%d\n",f_p->thread_id);
    usleep(80000);
    printf("OnceThread_Hardware_Init-end:%d\n",f_p->thread_id);
}

int counterb=0;
void CylicThread_DATA_exchange(struct cylic_thread_params *f_p)
{
    counterb++;
    printf("CylicThread_DATA_exchange:%d\n",counterb);
}
int counter1=0;
void CylicThread_Ctrl1(struct waitsem_thread_params *f_p)
{
    counter1++;
    printf("CylicThread_Ctrl1:%d\n",counter1);
}
int counter2=0;
void CylicThread_Ctrl2(struct waitsem_thread_params *f_p)
{
    counter2++;
    printf("CylicThread_Ctrl2:%d\n",counter2);
}

int main() 
{
    int i;

    char s[]="/home";
	memcpy(s_PROGRM_MAIN_PATH,s,strlen(s));



    otp_ECM_INIT.thread_id = 3;
	otp_ECM_INIT.once_task = OnceThread_Hardware_Init;//单次运行线程
	otp_ECM_INIT.CPU_idx = 0;//线程运行的cpu
	otp_ECM_INIT.priority=70;//优先级，越大优先级越高，最高好像是85
	build_RTOnce(&otp_ECM_INIT);//建立实时线程并立即执行（build_NRTOnce可建立非实时线程）


	/*建立基频倍频线程START*/
	InitCylicThread(&ctp_fast_task);//基频线程
	ctp_fast_task.thread_id = 4;
	ctp_fast_task.period_ns = 10*1000000;//基频线程10ms运行一次
	ctp_fast_task.cyclic_task = CylicThread_DATA_exchange;//CylicThread_DATA_exchange为运行的内容函数
	ctp_fast_task.ctrl = 0; // OnceThread_ECM_INIT启动完成后，再执行本线程的内容
	ctp_fast_task.timeout_warn=0;//超时警告（1打印警告信息）
	ctp_fast_task.CPU_idx = 1; //运行的CPU
	ctp_fast_task.priority=83;//优先级
    ctp_fast_task.bindsem_ftokid=256;//倍频函数用的信号量ID，前提是s_PROGRM_MAIN_PATH目录已经被设置

	wtsem_slow_follow_task1.thread_id = 5;//倍频线程
	wtsem_slow_follow_task1.waitsem_task = CylicThread_Ctrl1;
	wtsem_slow_follow_task1.CPU_idx = 2; //
	wtsem_slow_follow_task1.priority=75;
	
	wtsem_slow_follow_task2.thread_id = 6;//倍频线程
	wtsem_slow_follow_task2.waitsem_task = CylicThread_Ctrl2;
	wtsem_slow_follow_task2.CPU_idx = 2; //
	wtsem_slow_follow_task2.priority=75;

    bind_RTWaitSemTickThread(&ctp_fast_task,&wtsem_slow_follow_task1,5);//将wtsem_ctrl与ctp_fast_task以倍频绑定，占用semid0
	bind_RTWaitSemTickThread(&ctp_fast_task,&wtsem_slow_follow_task2,8);//将wtsem_ctrl与ctp_fast_task以倍频绑定,占用semid1
    bind_RTWaitSemTickThread(&ctp_fast_task,NULL,10);//给TestRTread_Sem.cc程序中的线程预留。其会按顺序占用semid2
    /*....可以继续加bind_RTWaitSemTickThread*/

	build_RTCylic(&ctp_fast_task);//只build ctp_fast_task，wtsem_ctrl会自动建立
	append_Cylic(&ctp_fast_task);//加入循环线程列别（可用来恢复特定的循环线程，无此需求可以不调用）

    /*建立基频倍频线程END*/


    sleep(5);

    *(ctp_fast_task.p_ctrl) = 1;//打开基频ctp_fast_task线程，同时会自动打开倍频CylicThread_Ctrl1、2

    int cnt=0;
    while (cnt<400)
    {
        usleep(10000);
        cnt++;
        
    }
    clearAllCylicThread();

    sleep(1);
    return 1;

}
